var util = require('../../../utils/util')
var api = require('../../../config/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    report: {},
    //审批流程记录
    reportRecord: [],
     //选择分管领导
     selectedCharge: {},
     selectedChargeindex: null,
     //已经选择的分管领导
    chargeLeader:{},
    //奖励金额
    rewards: 0,
    //任务名称
    taskName:null,
    //任务要求
    requirements:null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('row', function (data) {
      console.log(data)
      that.setData({
        report: data,
        imgList: (data.fliePath || '').split(';').filter(item => item !== ''),
        imgListed: (data.verifyImage || '').split(';').filter(item => item !== '')
      })
      //获取历史审批记录
      that.getRecord(data.reportId)
      //获取分管领导
      that.initChargeData();
    })

  },

  //分管领导
  initChargeData(){
    let that = this
    var roleCode = { roleCodeList: ['role_charge_leader'] }
    util.request(api.getVerifyUserlist, JSON.stringify(roleCode), 'POST').then(function (res) {
      
      if (res.code === "200") {
        that.setData({
          selectedCharge: res.data
        })

      } else {
        util.showErrorToast(res.message);

      }

    }).catch((err) => {
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });

  },
  
  /**
   *获取历史审批记录
   */
  getRecord(reportId) {
    let that = this
    var pageInfo = {
      reportId:reportId
    }
    that.loadModal();
    util.request(api.getReportRecord, JSON.stringify(pageInfo), 'POST').then(function (res) {
      console.log(res)
      if (res.code === "200") {
        that.closeModal();
        //刷新界面
        that.setData({
          reportRecord: res.data,
        });
      } else {
        util.showErrorToast(res.message);
        that.closeModal();
      }

    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });
  },


  //浏览图片
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  //浏览核实照片
  ViewImageed(e) {
    wx.previewImage({
      urls: this.data.imgListed,
      current: e.currentTarget.dataset.url
    });
  },
  
  //进度条
  loadModal() {
    this.setData({
      loadModal: true
    })
  },
  closeModal() {
    this.setData({
      loadModal: false
    })
  },

  //分管领导
  bindPickerChargeOrg(event) {
    let index = event.detail.value;
    this.data.chargeLeader = this.data.selectedCharge[index]
    this.setData({
      selectedChargeindex: index
    })
  },

  //-----------输入--------//
  bindTaskName(event) {
    this.data.taskName = event.detail.value
    
  },
  //-----------输入--------//
  bindRequirements(event) {
    this.data.requirements = event.detail.value
    
  },
  //-----------输入--------//
  bindinputRewards(event) {
    this.data.rewards = event.detail.value
    
  },


  //保存提交
  save() {
    if (this.data.taskName == '' || this.data.taskName == null) {
      util.showErrorToast('任务名称不能为空！');
      return false
    }
    if (this.data.requirements == '' || this.data.requirements == null) {
      util.showErrorToast('任务要求不能为空！');
      return false
    }
    if (this.data.rewards == '' || this.data.rewards == null) {
        util.showErrorToast('奖励金额不能为空！');
        return false
      }
      if (Object.keys(this.data.chargeLeader).length <= 0 ) {
        util.showErrorToast('分管领导不能为空！');
        return false
      }
  
    //提交上报数据
    //提交上报数据
    let dataup = {
        reportId: this.data.report.reportId,
        rewards: this.data.rewards,
        taskName: this.data.taskName,
        requirements: this.data.requirements,
        chargeLeaderId: this.data.chargeLeader.id
      };
    let that = this;
    console.log(JSON.stringify(dataup))
    that.loadModal();
    util.request(api.addTask, JSON.stringify(dataup), 'POST').then(function (res) {
      console.log(res)
      if (res.code == '200') {
        that.closeModal();
        //刷新界面
        wx.navigateBack()

      } else {
        util.showErrorToast(res.message);
        that.closeModal();
      }

    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });


  },

  
})